package com.example.demo.controller;

import com.example.demo.dto.UserDTO;
import com.example.demo.response.ResponseData;
import com.example.demo.service.UserService;
import com.google.common.base.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/{id}")
    public ResponseData<UserDTO> detail(@PathVariable Long id){

        Preconditions.checkNotNull(id, "id can't be null");

        return ResponseData.success(userService.getByUid(id));
    }

    @PostMapping("/create")
    public ResponseData<Long> create(UserDTO userDTO) {
        Preconditions.checkNotNull(userDTO, "userDTO can't be null");

        return ResponseData.success(userService.create(userDTO));
    }

    @PostMapping("/update")
    public ResponseData<Boolean> update(UserDTO userDTO) {
        Preconditions.checkNotNull(userDTO, "userDTO can't be null");

        return ResponseData.success(userService.update(userDTO));
    }

    @PostMapping("/delete")
    public ResponseData<Boolean> delete(Long id) {
        Preconditions.checkNotNull(id, "id can't be null");

        return ResponseData.success(userService.delete(id));
    }
}
